package com.java.api;


import com.java.config.result.Result;
import com.java.model.user.UserInfo;
import com.java.model.vo.LoginVo;
import com.java.model.vo.UserAuthVo;
import com.java.service.UserInfoService;
import com.java.utils.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    //手机号登录接口 {phone}/{code} 因为@RequestBody 传的时候报错 ，暂时先拿这个传
    @GetMapping("/login/{phone}/{code}")
    private Result loginUser(@PathVariable String phone,@PathVariable String code){
        LoginVo loginVo = new LoginVo();
        loginVo.setPhone(phone);
        loginVo.setCode(code);
        //获取用户传过来的phone 和 code
        Map<String,Object> result = userInfoService.login(loginVo);
        return Result.ok(result);
    }

    //用户认证接口
    @PostMapping("/auth/userAuth")
    public Result getUserInfo(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        //AuthContextHolder.getUserId(request) 这个是之前封装的方法 返回userid 和 username
       userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
       return Result.ok();
    }

    //根据用户id获取用户信息
    @GetMapping("/auth/getUser/")
    public Result getUserById(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
